package Queue;


    public class MyCircularQueue {

        int rear;//放
        int front;//取
        int[] elem;

        public MyCircularQueue(int k) {
            this.front=0;
            this.rear=0;
            this.elem=new int[k+1];
        }

        public boolean enQueue(int value) {
            if(isFull()) return false;
            this.elem[this.rear]=value;
            rear=(rear+1)%elem.length;
            return true;
        }

        public boolean deQueue() {
            if(isEmpty()) return false;
            this.front=(this.front+1)%this.elem.length;
            return true;
        }

        public int Front() {
            if(isEmpty()) return -1;

            return this.elem[front];
        }

        public int Rear() {
            if(isEmpty()){
                return -1;
            }
            int index=(this.rear==0?this.elem.length-1:this.rear-1);
            return elem[index];
        }

        public boolean isEmpty() {
            if(this.rear==this.front) return true;
            return false;
        }

        public boolean isFull() {
            if((this.rear+1)%this.elem.length==this.front){
                return true;
            }
            return false;
        }
    }
